package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.domain.Statement;
import com.ruoyi.business.domain.StatementItem;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.business.service.IStatementItemService;
import com.ruoyi.business.service.IStatementService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/business/statementItem")
class StatementItemController extends BaseController {
    private static final String prefix = "business/statementItem/";
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IServiceItemService itemService;

    /**
     * 详情页
     */
    @RequiresPermissions("business:statement:itemDetail")
    @GetMapping( "/itemDetail/{id}")
    public String itemDetail(@PathVariable("id")Long id, Model model) {
        if (id == null) {
            throw new ServiceException("参数传递错误");
        }
        Statement statement = statementService.selectStatementById(id);
        model.addAttribute("statement",statement);
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())){
            //当前为已经消费
            return prefix + "showDetail";
        }else {
            //当前为消费中
            return prefix + "editDetail";
        }
    }
    @RequestMapping("/queryForServiceItem")
    @ResponseBody
    public TableDataInfo queryForServiceItem(ServiceItem serviceItem){
        List<ServiceItem> serviceItems = itemService.selectServiceItemList(serviceItem);
        //不分页
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(serviceItems);
        return tableDataInfo;
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> saveItems){
        //进行储存
        itemService.saveItems(saveItems);
        return AjaxResult.success();
    }
    //对个人结算单数据进行查询
    @RequestMapping("/query/{statementId}")
    @ResponseBody
    public TableDataInfo query(@PathVariable("statementId") Long statementId){
        if(statementId == null){
            throw new ServiceException("参数传递错误");
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        List<StatementItem> list = statementItemService.query(statementId);
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }
     //对用户进行支付操作
    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult pay(Long statementId){
        if(statementId == null){
            throw new ServiceException("参数传递错误");
        }
        statementService.pay(statementId);
        //对支付业务需要响应信息
        AjaxResult success = AjaxResult.success();
        success.put(AjaxResult.CODE_TAG,Statement.STATUS_PAID);
        success.put(AjaxResult.MSG_TAG,"支付成功!");
        return success;
    }

}
